/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.examination.barbecue.user.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.zx.examination.barbecue.entity.TexaminationBarbecue;
import com.jeesite.modules.zx.examination.barbecue.service.TexaminationBarbecueService;
import com.jeesite.modules.zx.examination.barbecue.user.entity.TexaminationBarbecueUser;
import com.jeesite.modules.zx.examination.barbecue.user.service.TexaminationBarbecueUserService;

/**
 * 理论考试用户记录表Controller
 * @author zpp
 * @version 2020-03-07
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/examination/barbecue/user/texaminationBarbecueUser")
public class TexaminationBarbecueUserController extends BaseController {

	@Autowired
	private TexaminationBarbecueUserService texaminationBarbecueUserService;
	@Autowired
	private TexaminationBarbecueService texaminationBarbecueService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TexaminationBarbecueUser get(String id, boolean isNewRecord) {
		return texaminationBarbecueUserService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:examination:barbecue:user:view")
	@RequestMapping(value = {"list", ""})
	public String list(TexaminationBarbecueUser texaminationBarbecueUser, Model model) {
		model.addAttribute("texaminationBarbecueUser", texaminationBarbecueUser);
		return "modules/zx/examination/barbecue/user/texaminationBarbecueUserList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:examination:barbecue:user:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TexaminationBarbecueUser> listData(TexaminationBarbecueUser texaminationBarbecueUser, HttpServletRequest request, HttpServletResponse response) {
		texaminationBarbecueUser.setPage(new Page<>(request, response));
		Page<TexaminationBarbecueUser> page = texaminationBarbecueUserService.findPage(texaminationBarbecueUser);
		for(TexaminationBarbecueUser tb: page.getList()){
			String theoryId=tb.getTheoryId();
			TexaminationBarbecue texaminationBarbecue = texaminationBarbecueService.get(theoryId);
			if(texaminationBarbecue!=null){
			  tb.setTheoryName(texaminationBarbecue.getObjName());
			}
			String postId = tb.getPostId();
			TexaminationBarbecue texaminationBarbecue2 = texaminationBarbecueService.get(postId);
			if(texaminationBarbecue2!=null){
				tb.setPostName(texaminationBarbecue2.getObjName());
			}
			
			String userId = tb.getUserId();
			User user = UserUtils.get(userId);
			if(user!=null){
			  tb.setUserName(user.getUserName());
			}
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:examination:barbecue:user:view")
	@RequestMapping(value = "form")
	public String form(TexaminationBarbecueUser texaminationBarbecueUser, Model model) {
		model.addAttribute("texaminationBarbecueUser", texaminationBarbecueUser);
		return "modules/zx/examination/barbecue/user/texaminationBarbecueUserForm";
	}

	/**
	 * 保存理论考试用户记录表
	 */
	@RequiresPermissions("zx:examination:barbecue:user:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TexaminationBarbecueUser texaminationBarbecueUser) {
		texaminationBarbecueUserService.save(texaminationBarbecueUser);
		return renderResult(Global.TRUE, text("保存理论考试用户记录表成功！"));
	}
	
	/**
	 * 停用理论考试用户记录表
	 */
	@RequiresPermissions("zx:examination:barbecue:user:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TexaminationBarbecueUser texaminationBarbecueUser) {
		texaminationBarbecueUser.setStatus(TexaminationBarbecueUser.STATUS_DISABLE);
		texaminationBarbecueUserService.updateStatus(texaminationBarbecueUser);
		return renderResult(Global.TRUE, text("停用理论考试用户记录表成功"));
	}
	
	/**
	 * 启用理论考试用户记录表
	 */
	@RequiresPermissions("zx:examination:barbecue:user:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TexaminationBarbecueUser texaminationBarbecueUser) {
		texaminationBarbecueUser.setStatus(TexaminationBarbecueUser.STATUS_NORMAL);
		texaminationBarbecueUserService.updateStatus(texaminationBarbecueUser);
		return renderResult(Global.TRUE, text("启用理论考试用户记录表成功"));
	}
	
	/**
	 * 删除理论考试用户记录表
	 */
	@RequiresPermissions("zx:examination:barbecue:user:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TexaminationBarbecueUser texaminationBarbecueUser) {
		texaminationBarbecueUserService.delete(texaminationBarbecueUser);
		return renderResult(Global.TRUE, text("删除理论考试用户记录表成功！"));
	}
	
}